package cn.edu.ujn.lab3.service.impl;


import cn.edu.ujn.lab3.entity.Customer;
import cn.edu.ujn.lab3.entity.Page;
import cn.edu.ujn.lab3.mapper.CustomerMapper;
import cn.edu.ujn.lab3.mapper.userMapper;
import cn.edu.ujn.lab3.service.ICustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.List;

@Service
public class CustomerServiceImpl implements ICustomerService {

    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private userMapper userMapper;
    @Override
    public List<Customer> findAll() {
        List<Customer> customerList = customerMapper.findAll();
        return customerList;
    }

    @Override
    public List<Customer> selectByMobile(String mobile) {
        List<Customer> customer = customerMapper.selectByMobile(mobile);
        return customer;
    }

    @Override
    public List<Customer> selectCustomerByName(String name) {
        List<Customer> customer = customerMapper.selectCustomerByName(name);
        return customer;
    }

    @Override
    public List<Customer> selectCustomerByPage(Page page) {
        List<Customer> customers = customerMapper.selectCustomerByPage(page);
        return customers;
    }

    @Override
    public Boolean addCustomer(Customer customer) {
        Date date = new Date(System.currentTimeMillis());
        customer.setCreateTime(date);
        Boolean flag =true;
        System.out.println(userMapper.selectCustomerByUserId(customer));
        System.out.println(userMapper.selectCustomerByCreateId(customer));
        System.out.println(customer);
        if (userMapper.selectCustomerByUserId(customer)==null || userMapper.selectCustomerByCreateId(customer)==null){
            flag=false;
        }else {
            customerMapper.addCustomer(customer);
        }
        return flag;
    }

    @Override
    public Boolean updateCustomer(Customer customer) {
        Date date = new Date(System.currentTimeMillis());
        customer.setCreateTime(date);
        Boolean flag = customerMapper.updateCustomer(customer);
        return flag;
    }

    @Override
    public Boolean deleteCustomer(Integer id) {
        Boolean flag = customerMapper.deleteCustomer(id);
        return flag;
    }
}
